package org.test.client.ui;

import java.awt.CardLayout;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import org.test.client.ui.component.MyBasePanel;
import org.test.client.ui.content.dashboard.DashboardPanel;
import org.test.client.ui.util.run.RunCallBack;

public class ContentMain extends JPanel {
	private ContentMain thisPanel = this;
	private MainPanel mainPanel = null;
	
	private CardLayout cardLayout = null;
	private Map<String, MyBasePanel> panelMap = null;
	private Map<String, String> panelClass = null;
	
	private MyBasePanel currentPanel = null;
	private MyBasePanel oldPanel = null;
	
	public ContentMain(MainPanel main) {
		this.mainPanel = main;
		
		doInit();
	}
	
	public ContentMain doInit(){
		
		cardLayout = new CardLayout();
		this.setLayout(cardLayout);
		

		panelMap = new HashMap<String, MyBasePanel>();
		panelClass = new HashMap<String, String>();
		
		MyBasePanel dashboardPanel = new DashboardPanel(this).doInit();
		panelMap.put(dashboardPanel.getPanelKey(), dashboardPanel);
		
		currentPanel = dashboardPanel;
		oldPanel = dashboardPanel;
		
		this.add(dashboardPanel,dashboardPanel.getPanelKey());
		
		loadOtherPanel();
		
		return this;
	}
	
	public void loadOtherPanel(){
		panelClass.put("DashBoard", "org.test.client.ui.content.dashboard.DashboardPanel");
		panelClass.put("ChartDemo", "org.test.client.ui.content.chartdemo.ChartDemo");
		panelClass.put("FormsDemo", "org.test.client.ui.content.forms.FormsDemo");
		panelClass.put("UIFeature", "org.test.client.ui.content.uifeature.UIFeature");
		panelClass.put("TableDemo", "org.test.client.ui.content.table.TableDemo");
	}
	
	public void changePanel(final String key) throws Exception{
		MyBasePanel myBasePanel = panelMap.get(key);
		if(myBasePanel==null){
			String classStr = panelClass.get(key);
			MyBasePanel tmpPanel = (MyBasePanel)Class.forName(classStr).newInstance();
			panelMap.put(key, tmpPanel);
			this.add(tmpPanel,key);
			currentPanel = tmpPanel;
		}else if(myBasePanel == currentPanel){
//			System.out.println("已经选中");
			return ;
		}else{
			currentPanel = myBasePanel;
		}
		
		oldPanel.fadeOut(new RunCallBack() {
			@Override
			public Object doAfterRun(Object obj) {
				cardLayout.show(thisPanel, key);
				currentPanel.fadeIn(null);
				oldPanel = currentPanel;
				return null;
			}
		});
		
	}

	public MainPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(MainPanel main) {
		this.mainPanel = main;
	}

	public CardLayout getCardLayout() {
		return cardLayout;
	}

	public void setCardLayout(CardLayout cardLayout) {
		this.cardLayout = cardLayout;
	}

	public Map<String, MyBasePanel> getPanelMap() {
		return panelMap;
	}

	public void setPanelMap(Map<String, MyBasePanel> panelMap) {
		this.panelMap = panelMap;
	}

	public Map<String, String> getPanelClass() {
		return panelClass;
	}

	public void setPanelClass(Map<String, String> panelClass) {
		this.panelClass = panelClass;
	}

	public MyBasePanel getCurrentPanel() {
		return currentPanel;
	}

	public void setCurrentPanel(MyBasePanel currentPanel) {
		this.currentPanel = currentPanel;
	}
	
}
